/*
 *
 * @author Code and Pseudocode: Rob Brown, Shaun Pickford
 * This class prompts the user for the starting position of each ship starting with the largest (Aircraft Carrier).
 * It then displays the possible end points for that ship based on the start position.
 */

package battleship;

import java.util.*;
import javax.swing.JOptionPane;




public class PlaceShips {

	String[] ships = {"Aircraft Carrier", "Battleship", "Carrier", "Submarine", "Destroyer"};
	String[] directions;
	int[] endPoints;
	
	int startPosition, endPosition;
	
	ShipPlacement ShipPlacement = new ShipPlacement();
	Conversion convert = new Conversion();
	Ship ship;
	
	public void placeUserShips() {
		
		for(int x = 0; x < 5; x++ ){

                        boolean validNumber = false;
                        while(!validNumber)
                        {
                            startPosition = convert.toNumber(JOptionPane.showInputDialog("Choose a starting location for your " + ships[x]));
                            if(startPosition == -1)
                            {
                                validNumber = false;
                            }
                            else
                            {
                                validNumber = true;
                            }
                        }
                        boolean validStartPoint = false;
                        while(!validStartPoint)
                        {

                            if(startPosition == -1)
                            {
                                validStartPoint = false;
                            }
                            else
                            {
                                validStartPoint = true;
                            }

                            if(Main.gameBoard.gameBoard[startPosition - 1] != 'N')
                            {
                                validStartPoint = false;
                                boolean validNum = false;
                                while(!validNum)
                                {
                                    startPosition = convert.toNumber(JOptionPane.showInputDialog(null, "Already a ship at that start point! Please choose another starting point! \n\nChoose a starting location for your " + ships[x]));
                                    if(startPosition == -1)
                                    {
                                        validNum = false;
                                    }
                                    else
                                    {
                                        validNum = true;
                                    }
                                }
                            }
                            else
                            {
                            	validStartPoint = true;
                            }
                        }
			// Prompt user for start position of ship
			
			// Create new ship object, get endpoints, and directions.
			ship = new Ship(ships[x], shipSize(ships[x]));
                        ShipPlacement.checkShipPlacement(ship, startPosition, 'p');
			endPoints = ShipPlacement.getPossibleEndPoints();
			directions = ShipPlacement.getPossibleDirections();
			
			// Get possible endpoints.
			int temp = 0;
			String[] possibilities = new String[4]; // Stores all the possible endpoints in a string for the end user.
			
			for(int y = 0; y < endPoints.length; y++){
				if(endPoints[y] > -1){
                                        //System.out.println("endPoints[" + y +"] is " + endPoints[y]);
					possibilities[temp] = convert.toCoordinate(endPoints[y]);
					temp += 1;
				}
			}

                        for(int xx = 0; xx < possibilities.length; xx++)
                        {
                            if(possibilities[xx] == null)
                            {
                                possibilities[xx] = "";
                            }
                            else
                            {
                                possibilities[xx] = possibilities[xx] + "  ";
                            }
                        }

                        //endPosition = convert.toNumber(JOptionPane.showInputDialog(null, "Select a coordinate for your ending position: " + possibilities[0] + possibilities[1] + possibilities[2] + possibilities[3]));

                        String shipDirection = null;
                        boolean validEndPoint = false;
                        endPosition = convert.toNumber(JOptionPane.showInputDialog(null, "Select a coordinate for your ending position: " + possibilities[0] + possibilities[1] + possibilities[2] + possibilities[3]));

                        for(int z = 0; z < possibilities.length; z++)
                        {
                            if((endPoints[z]) == endPosition)
                            {
                                shipDirection = directions[z];
                                validEndPoint = true;
                                break;
                            }
                            else
                            {
                                validEndPoint = false;
                            }
                        }
                        
                        while(!validEndPoint)
                        {
                            /*if(!validEndPoint)
                            {
                                JOptionPane.showInputDialog(null, "You did not choose a valid ending position! \n\nSelect a coordinate for your ending position: " + possibilities[0] + possibilities[1] + possibilities[2] + possibilities[3]);
                            }*/
                            
                            for(int z = 0; z < possibilities.length; z++)
                            {
				if((endPoints[z]) == endPosition)
                                {
                                    shipDirection = directions[z];
                                    validEndPoint = true;
                                    break;
				}
                                else
                                {
                                    validEndPoint = false;
                                }
                            }
                            if(!validEndPoint)
                            {
                                endPosition = convert.toNumber(JOptionPane.showInputDialog(null, "You did not choose a valid ending position! \n\nSelect a coordinate for your ending position: " + possibilities[0] + possibilities[1] + possibilities[2] + possibilities[3]));
                            }
                            
                        }//while endpoint is invalid

                        /*if(!validEndPoint)
                        {
                            endPosition = convert.toNumber(JOptionPane.showInputDialog(null, "You did not choose a valid ending position! \n\nSelect a coordinate for your ending position: " + possibilities[0] + possibilities[1] + possibilities[2] + possibilities[3]));
                        }*/


			ShipPlacement.placePlayerShips(shipDirection, startPosition, endPosition, ship);
		}
	}
	
	public int shipSize(String ship){
		
	   /*
		* A = Aircraft Carrier = 5 hits
		* B = Battleship = 4 hits
		* C = Carrier = 3 hits
		* S = Submarine = 3 hits
		* D = Destroyer = 2 hitS
		*/
		
		int size = -1;
		
		if(ship.equals("Aircraft Carrier")){
			size = 5;
		}
		else if(ship.equals("Battleship")){
			size = 4;
		}
		else if(ship.equals("Carrier")){
			size = 3;
		}
		else if(ship.equals("Submarine")){
			size = 3;
		}
		else if(ship.equals("Destroyer")){
			size = 2;
		}
		
		return size;
	}
}
